//
//  RegisterViewController.m
//  豆瓣HW
//
//  Created by dllo on 15/11/26.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "RegisterViewController.h"
#import "RegisterView.h"
#import "DataBaseHandle.h"
#import "User.h"

@interface RegisterViewController ()

@property (nonatomic, retain) RegisterView *registerV;

@end

@implementation RegisterViewController

- (void)dealloc
{
    [_registerV release];

    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.navigationItem.title = @"注册";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(barAction:)];
    
    self.registerV = [[RegisterView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.registerV];
    [_registerV release];
}



- (void)barAction:(UIBarButtonItem *)sender
{
    NSLog(@"%@", NSHomeDirectory());
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

    NSString *filePath = [path stringByAppendingPathComponent:@"str.xml"];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
        if ([dic.allKeys containsObject:self.registerV.textF1.text]) {
//            NSLog(@"该用户已经注册");
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已被注册" preferredStyle:UIAlertControllerStyleAlert];
            [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                
            }]];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            
        } else {
            
            if (self.registerV.textF2.text.length != 0 && self.registerV.textF3.text.length != 0) {
                
                // 注册成功,存入xml,打开数据库,操作数据库
                if ([self.registerV.textF2.text isEqualToString:self.registerV.textF3.text]) {
                    [dic setObject:self.registerV.textF2.text forKey:self.registerV.textF1.text];
                    [dic writeToFile:filePath atomically:YES];
                    
                    User *user = [[User alloc] init];
                    user.name = self.registerV.textF1.text;
                    user.password = self.registerV.textF2.text;
                    user.state = @"1";

                    
//                    [[DataBaseHandle shareDataBase] openDB];
                    [[DataBaseHandle shareDataBase] createUserTable];
                    [[DataBaseHandle shareDataBase] insertDataWithUser:user];
                    [[DataBaseHandle shareDataBase] createActivityTable];
                    [[DataBaseHandle shareDataBase] createMovieTable];
                    [self.navigationController popViewControllerAnimated:YES];
                } else {
//                    NSLog(@"两次输入密码不同,请重新输入!");
                    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入密码不同,请重新输入!" preferredStyle:UIAlertControllerStyleAlert];
                    [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                        
                        
                    }]];
                    [self presentViewController:alertC animated:YES completion:^{
                        
                        
                    }];
                }
                
            } else {
//                NSLog(@"请输入密码并确认!");
                UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入密码并确认!" preferredStyle:UIAlertControllerStyleAlert];
                [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                    
                }]];
                [self presentViewController:alertC animated:YES completion:^{
                    
                    
                }];
            }
        }
}
    
    
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
